"""
@author: tian-yan-tian-ge
@file: sanchuang.py
@time: 2023/12/6 10:00
"""
import requests
import time
import logging


# 获取文本
def download(url):
    #
    headers = {
        "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/119.0.0.0"
    }
    try:
        # try内部写有可能会出错的代码
        response = requests.get(url, headers=headers)
        response.raise_for_status()
        # 设置编码格式
        response.encoding = response.apparent_encoding
        # 返回文本数据
        time.sleep(3)
        print(url, "访问成功")
        return response.text
    except:
        # 如果出错了会执行这里

        print(url, "访问错误")
        return ""


# 获取二进制
def download_img(url):
    #
    headers = {
        "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/119.0.0.0"
    }
    try:
        # try内部写有可能会出错的代码
        response = requests.get(url, headers=headers)
        response.raise_for_status()
        # 设置编码格式
        response.encoding = response.apparent_encoding
        # 返回文本数据
        time.sleep(3)
        print(url, "访问成功")
        return response.content
    except:
        # 如果出错了会执行这里
        print(url, "访问错误")
        return ""
